package com.baoer.util;

import com.baoer.R;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/**
 * @Author: YING
 * @CreateTime: 2025-07-01
 * @Description:
 * @Version: 1.0
 */


public class iconutil {
    public static R uploadFile(MultipartFile file,String pathtype) {
        //判断文件是否为空
        if(file.isEmpty()){
            return R.er();
        }
        //获取传过来的文件名字
        String OriginalFilename = file.getOriginalFilename();
        //获取系统时间戳+原始文件的后缀名
        String fileName = System.currentTimeMillis() + "."+OriginalFilename.substring(OriginalFilename.lastIndexOf(".")+1);
        //设置保存地址
        //1.后台保存位置
        String path = "D:/image/"+pathtype;
        File dest = new File(path+fileName);
        //判断文件是否存在
        if(!dest.getParentFile().exists()){
            //不存在就创建
            dest.getParentFile().mkdirs();
        }
        try{
            //后台上传
            file.transferTo(dest);
            return R.ok(pathtype+fileName);
        } catch (Exception e) {
            e.printStackTrace();
            return R.er();
        }
    }
}
